//
//  HMListenLoadAudioViewController.m
//  HowToDo
//
//  Created by mynSoo on 2017/1/8.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMListenLoadAudioViewController.h"
#import "HMLisenAudioLoadingView.h"
#import "HMAudioPlayerController.h"
#import "HMListenAnyTimeResViewModel.h"


@interface HMListenLoadAudioViewController ()
@property (nonatomic, weak) HMLisenAudioLoadingView *loadingview;

@property (nonatomic, weak) UILabel *textlabel;

@property (nonatomic, weak) UILabel *subTextlabel;

@property (nonatomic, assign) BOOL isCancle;
@end

@implementation HMListenLoadAudioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self installUI];
    
    self.isCancle = NO;
    
}


-(void)setModelArrary:(NSArray *)modelArrary
{
    _modelArrary = modelArrary;
    
    self.loadingview.finishBlock = ^(UIView *progressView){
    
        if(self.finishBlock)
        {
            self.textlabel.text = @"已经为你精选";
            
            self.subTextlabel.text = [NSString stringWithFormat:@"%ld条音频",self.modelArrary.count];
    
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:^{
                    self.finishBlock(progressView);
                }];
            });
        }
    };
    
    __weak typeof(self) weakSelf = self;

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSArray *durationArray = [weakSelf loadURLAsset:weakSelf.modelArrary];
        
        // [weakSelf.loadingview animate:durationArray];
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
           // NSArray *durationArray = [weakSelf loadURLAsset:weakSelf.modelArrary];
            
            [weakSelf.loadingview animate:durationArray];
        });
    });
    // load
    
    // self.loadingview.modelArr = modelArrary;
    
    // [self.loadingview animateProgress];

}

- (void)installUI
{
    self.view.backgroundColor = [UIColor clearColor];
    
    UIView *centerView = [self installCenterView];
    
    [self.view addSubview:centerView];
    
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
        make.height.equalTo(self.view).multipliedBy(0.58);
        make.width.equalTo(self.view).multipliedBy(0.87);
    }];
    
    

}

- (UIView *)installCenterView
{
    UIView *centerView = [[UIView alloc]init];
    centerView.backgroundColor = [UIColor whiteColor];
    
    centerView.layer.cornerRadius = 20;
    
    
    //
    UIImageView *iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"recommending"]];
    
    [centerView addSubview:iconImageView];
    
    
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(40);
        make.centerX.offset(0);
    }];
    
    //
    UILabel *textLabel = [[UILabel alloc]init];
    self.textlabel = textLabel;
    textLabel.text = @"正在为您精选音频";
    [centerView addSubview:textLabel];
    
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconImageView.mas_bottom).offset(28);
        make.centerX.offset(0);
    }];
    
    UILabel *subTextLabel = [[UILabel alloc]init];
    self.subTextlabel = subTextLabel;
    subTextLabel.text = @"精选中...";
    [centerView addSubview:subTextLabel];
    
    [subTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(11);
        make.centerX.offset(0);
    }];
    
    //38
    
    UIView *seperatorView = [[UIView alloc]init];
    
    seperatorView.backgroundColor = [UIColor blackColor];

    [centerView addSubview:seperatorView];
    
    [seperatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(subTextLabel.mas_bottom).offset(38);
        make.left.offset(20);
        make.right.offset(-20);
        make.height.offset(1.0 /[UIScreen mainScreen].scale);
        
    }];
    
    HMLisenAudioLoadingView *loadingView = [[HMLisenAudioLoadingView alloc]init];
    
    [centerView addSubview:loadingView];
    self.loadingview = loadingView;
    [loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(seperatorView);
        make.bottom.equalTo(seperatorView.mas_top).offset(-20);
        make.height.offset(10);
    }];
    
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
//    [cancleBtn setImage:[UIImage imageNamed:@"subscribe_audio_pop_bg"] forState:UIControlStateNormal];
    
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:[UIColor colorWithHex:0xaaaaaa] forState:UIControlStateNormal];
    cancleBtn.layer.cornerRadius= 12;
    cancleBtn.layer.masksToBounds = YES;
    cancleBtn.layer.borderWidth = 1;
    cancleBtn.layer.borderColor = [UIColor colorWithHex:0xe2e2e2].CGColor;
    
    [centerView addSubview:cancleBtn];
    
    
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(seperatorView.mas_bottom).offset(30);
        make.width.offset(150);
        make.height.offset(50);
        make.centerX.offset(0);
        
    }];
    
    [cancleBtn addTarget:self action:@selector(clickCancleBtn:) forControlEvents:UIControlEventTouchDown];
    
    
    return centerView;
}


- (void)clickCancleBtn:(UIButton *)sender
{
    self.isCancle = YES;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSArray *)loadURLAsset:(NSArray *)modelArray
{
    NSMutableArray *arrM = [NSMutableArray array];
    float total = 0;
    
    for(NSInteger i = 0; i < modelArray.count; i++)
    {
        if(self.isCancle)
        {
            return nil;
        }
        // 修复动画加载
        
        HMListenAnyTimeResModel *model = modelArray[i];
        
        AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString:model.audio_file_url]];
        
        model.asset = asset;
        model.duration = CMTimeGetSeconds(asset.duration);
        total += model.duration;
    }
    
    for(NSInteger i = 0; i < modelArray.count; i++)
    {
        HMListenAnyTimeResModel *model = modelArray[i];
        
        NSNumber *duration = @(model.duration / total);
        
        [arrM addObject:duration];
    }
    return arrM.copy;
}
@end
